package com.arcpan.Controller;

import com.arcpan.Constant.BaseConstant;
import com.arcpan.Entity.Dto.SessionWebUserDto;
import com.arcpan.Entity.Query.FileShareQuery;
import com.arcpan.Mapper.FileShareMapper;
import com.arcpan.Result.PageResult;
import com.arcpan.Result.Result;
import com.arcpan.Service.FileShareService;
import com.arcpan.annotation.GlobalInterceptor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@RequestMapping("/share")
@RestController
public class FileShareController {
    @Resource
    private FileShareService fileShareService;

    @RequestMapping("/loadShareList")
    @GlobalInterceptor(checkLogin = true)
    public Result loadShareList(HttpSession httpSession,Integer pageNo,Integer pageSize){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.SESSION_KEY);
    String userId=sessionWebUserDto.getUserId();
        FileShareQuery fileShareQuery=new FileShareQuery();
        fileShareQuery.setOrderBy("share_time desc");
        fileShareQuery.setUserId(userId);
        PageResult pageResult=fileShareService.getBypage(fileShareQuery,pageNo,pageSize);
        return Result.success(pageResult);
    }

    @RequestMapping("/shareFile")
    @GlobalInterceptor(checkLogin = true)
    public Result shareFile(HttpSession httpSession,String fileId,Integer validType,String code){
      SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.SESSION_KEY);
      String userId=sessionWebUserDto.getUserId();
        fileShareService.sharefile(userId,fileId,validType,code);

        return Result.success();

    }
    @RequestMapping("/cancelShare")
    @GlobalInterceptor(checkLogin = true)
    public Result cancelShare(String shareIds,HttpSession httpSession){
     SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.SESSION_KEY);
     String userId=sessionWebUserDto.getUserId();
        fileShareService.cancel(shareIds,userId);
        return Result.success();

    }
}
